package cn.chenyu.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Crawler {
    public  Crawler(){
        coding = "utf-8";
    }
    public static Crawler crawler = null;
    public static String coding = null;
    public static void InitCrawler(){
        crawler = new Crawler();
    }

    /**
     * 获取表达式中的内容1
     * @param expression 表达式
     * @param line 原文
     * @return 所需内容
     * 未匹配则返回 空字符
     */
    public static String Grin(String expression,String line){
        //表达式
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            matcher.reset();
            while (matcher.find()) {
                return matcher.group(0); //获取第一个括号内容
            }
        }
        return "";
    }

    /**
     * 获取表达式中的内容1
     * @param expression 表达式
     * @param line 原文
     * @return 所需内容
     * 未匹配则返回 空字符
     */
    public static List<String> Grins(String expression,String line){
        List<String> list = new ArrayList<String>(30);
        String[] content=new String[10];
        Pattern pattern=Pattern.compile(expression);
        Matcher m=pattern.matcher(line);
        while(m.find()){
            list.add(m.group(1));
        }
        return list;
    }

    /**
     * 下载图片
     * @param urlList 地址集合
     * @param path 保存地址
     */
    public static void Download(List<String> urlList,String path) {
        int num = 1;
        //网址对象
        URL url = null;
        //创建字节流
        FileOutputStream fileOutputStream = null;
        //打开网址
        DataInputStream dataInputStream = null;
        //遍历网址地址集合
        for (String urlString : urlList) {
            try {
                //实例网址对象
                url = new URL(urlString);
                dataInputStream = new DataInputStream(url.openStream());
                //定义图片名/格式
                String imageName = num + ".jpg";
                fileOutputStream =  new FileOutputStream(path+imageName,true);
                num++;
                //转二进制
                byte[] buffer = new byte[1024];
                int length;
                //保存
                while ((length = dataInputStream.read(buffer)) > 0) {
                    //写入
                    fileOutputStream.write(buffer, 0, length);
                }
                //关闭
                dataInputStream.close();
                fileOutputStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    dataInputStream.close();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取网页源码
     * @param path 网址
     * @return	List
     * @throws Exception
     */
    public static List<String> runs(String path) throws Exception {
        List<String> urlList = new ArrayList<String>();
        // 根据链接（字符串格式），生成一个URL对象
        URL url = new URL(path);
        // 打开URL//网址--并打开
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        // 得到输入流，即获得了网页的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream(), "utf-8"));
        String line; // 读取输入流的数据，并显示
        //获取网页内容
        while ((line = reader.readLine()) != null) {
            if(!line.trim().equals("")){
                urlList.add(line);
            }
        }
        return urlList;
    }

    /**
     * 获取网页源码
     * @param path 网址
     * @return	List
     * @throws Exception
     */
    public static String runs1(String path) throws Exception {
        // 根据链接（字符串格式），生成一个URL对象
        URL url = new URL(path);
        // 打开URL//网址--并打开
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        // 得到输入流，即获得了网页的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream(), "utf-8"));
        String line; // 读取输入流的数据，并显示
        String str = ""; // 读取输入流的数据，并显示
        //获取网页内容
        while ((line = reader.readLine()) != null) {
            if(!line.trim().equals("")){
                str+=line;
            }
        }
        return str;
    }

    /**
     * 以用户登录方式强行访问(实例)
     * @param str
     * @throws IOException
     */
    public static void ddc(String str) throws IOException{
        URL url;
        int responsecode;
        HttpURLConnection urlConnection=null;
        BufferedReader reader=null;
        String line;
        try{
            //生成一个URL对象，要获取源代码的网页地址为：http://www.sina.com.cn
            url=new URL(str);
            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setRequestProperty("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)"); //防止报403错误。
            //获取服务器响应码
            responsecode=urlConnection.getResponseCode();
            if(responsecode==200){
                //响应成功
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
                while((line=reader.readLine())!=null){
                    //逐行获取内容
                }
            }
            else{
                System.out.println("获取不到网页的源码，服务器响应代码为："+responsecode);
            }
        }
        catch(Exception e){
            System.out.println("获取不到网页的源码,出现异常："+e);
        }
        finally{
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            urlConnection.disconnect();
        }
    }

    public static List<String> getStrList(List<String> strList , String reg){
        for (int i = 0; i < strList.size(); i++) {
            if(strList.get(i) != null && !strList.get(i).trim().equals("")){
                String str  = strList.get(i);
                str = Grin(reg,str);
                str = str.substring(1,str.length()-4);
                strList.set(i, str);
            }
        }
        return strList;
    }
}
	

